// Copyright (c) 2025, 大连理工大学 (Dalian University of Technology)
//
// Licensed under the Mulan PSL v2.
// You can use this file according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of the License at
//
//     http://license.coscl.org.cn/MulanPSL2
//
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. SEE THE MULAN PSL v2
// FOR MORE DETAILS.

/*
 * @Author: qiulin 1746365080@qq.com
 * @Date: 2024-11-04 09:35:11
 * @LastEditors: qiulin_JXGM ql1746365080@gmail.com
 * @LastEditTime: 2024-12-06 19:54:21
 * @FilePath: /SGSim/DBManager/Service/Input/Node/SPOINTService.cpp
 */

#include "DBManager/Service/Input/Node/PointService.h"

#include <cassert>
#include <cstddef>
#include <vector>

#include "DataStructure/Common/Out.h"

#include "DBManager/Common/ServiceMacro.h"
#include "DBManager/IRepository/Input/Node/IPointRepository.h"

using namespace SG::DBManager;
using namespace SG::DataStructure::FEM;

// 注册服务
SERVICE_REG_IMPL1 (PointService, IPointRepository)

PointService::PointService (IPointRepository& repos) : m_repository (repos)
{}

PointService::~PointService () = default;

SG::DataStructure::Common::Real PointService::GetOverallHeighth (const std::vector<SG::DataStructure::Common::Id>& ids)
{
    std::vector<SG::DataStructure::Common::Id> allIds;
    allIds.reserve(ids.size());

    SG::DataStructure::Common::Id curId, nextId;
    
    allIds.push_back(ids[0]);
    for(size_t i = 0; i < ids.size() - 1; i++)
    {
        curId = ids[i];
        nextId = ids[i + 1];
        if(nextId < 0)
        {
            for(SG::DataStructure::Common::Id j = curId + 1; j <= -nextId; j++)
            {
                allIds.push_back(j);
            }
        }
        else{
            allIds.push_back(nextId);
        }
    }


    SG::DataStructure::FEM::PointData pd;
    SG::DataStructure::Common::Real   maxy = std::numeric_limits<SG::DataStructure::Common::Real>::min();
    for (const SG::DataStructure::Common::Id& id : allIds)
    {
        m_repository.FindById (id, pd);
        if (maxy < pd.m_X[1])
            maxy = pd.m_X[1];
    }

    return maxy;
}
SG::DataStructure::Common::Real PointService::GetOverallWidth (const std::vector<SG::DataStructure::Common::Id>& ids)
{
        std::vector<SG::DataStructure::Common::Id> allIds;
    allIds.reserve(ids.size());

    SG::DataStructure::Common::Id curId, nextId;
    
    allIds.push_back(ids[0]);
    for(size_t i = 0; i < ids.size() - 1; i++)
    {
        curId = ids[i];
        nextId = ids[i + 1];
        if(nextId < 0)
        {
            for(SG::DataStructure::Common::Id j = curId + 1; j <= -nextId; j++)
            {
                allIds.push_back(j);
            }
        }
        else{
            allIds.push_back(nextId);
        }
    }

    SG::DataStructure::FEM::PointData pd;
    SG::DataStructure::Common::Real   maxx = std::numeric_limits<SG::DataStructure::Common::Real>::min();
    for (const SG::DataStructure::Common::Id& id : allIds)
    {
        m_repository.FindById (id, pd);
        if (maxx < pd.m_X[0])
            maxx = pd.m_X[0];
    }

    return maxx;
}